package com.study.services;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.mdao.HighSeasMapper;

@Service
@Transactional(rollbackOn = Exception.class)
public class HighSeasService {

	@Autowired
	HighSeasMapper mapper;

	/**
	 * 分页条件展示公海数据
	 * 
	 * @return
	 */
	public List<JSONObject> getgh(int pageNum, int pageSize, String input, String selects, String useid) {
		PageHelper.startPage(pageNum, pageSize);
		selectNot();
		System.out.println("分配完");
		String name = "%" + input + "%";
		List<JSONObject> selectgh = mapper.selectgh(name, selects, useid);
		return selectgh;
	}

	/**
	 * 查询出所有已分配出的公海客户,并作出相对应的操作
	 */
	public void selectNot() {
		// 查询出所有的已分配出的公海客户
		List<JSONObject> list = mapper.selectNoHigh();
		for (JSONObject js : list) {
			String cusid = js.getString("cus_id");// 获取循环的客户id
			Date ghdate = js.getDate("cus_ghdate");// 获取循环客户的领取时间
			int day = js.getInteger("high_day");// 获取该公海的回归期限
			Calendar start = new GregorianCalendar();
			start.setTime(ghdate);
			start.add(Calendar.DAY_OF_WEEK, day);
			Calendar end = Calendar.getInstance();
			if (start.before(end)) {
				List<JSONObject> dates = selectOrder(cusid);
				if (dates.size() == 0) {
					flyback(cusid);
				} else {
					for (int i = 0; i < dates.size(); i++) {
						Calendar start1 = new GregorianCalendar();
						//获取当前订单的创建时间
						start1.setTime(dates.get(i).getDate("o_cdate"));
						start.add(Calendar.WEDNESDAY, -day);
						if (start1.after(start)&&start1.before(end)) {
							priv(cusid);
							break;
						}else if (i==dates.size()-1) {
							flyback(cusid);
						}
					}
				}
			}
		}
	}

	/**
	 * 查询客户的订单
	 */
	public List<JSONObject> selectOrder(String cusid) {
		System.out.println("查看订单");
		return mapper.selectOrder(cusid);
	}
	
	
	/**
	 * 将已经超时的公海客户回归
	 */
    public boolean flyback(String cusid) {
    	System.out.println("开始回归客户");
    	Integer useid = null;
    	Date date = null;
    	return mapper.flydate(cusid,useid,date);
    }
	
	/**
	 * 将存在订单的公海客户改为私有客户
	 */
    public void priv(String cusid) {
    	 mapper.priv(cusid);
    }
    
	/**
	 * 新建公海
	 * 
	 * @param name
	 * @param day
	 * @param num1
	 * @param user
	 * @return
	 */
	public int addhigh(String name, String day, String num1, String user) {
		int day1 = Integer.parseInt(day);
		int num = Integer.parseInt(num1);
		int result = mapper.addhigh(name, day1, num, user, new Date(), 1);
		return 0;
	}
	
	

	public List<JSONObject> selecthigh(String useid) {

		return mapper.selecthigh(useid);

	}

	/**
	 * 分配员工
	 * 
	 * @param cusid
	 * @param uid
	 * @return
	 */
	public boolean upforHigh(String cusid, String uid) {
		boolean flag = false;
		// 根据客户找到对应的公海
		JSONObject js = mapper.selecgh1(cusid);
		String a = js.getString("high_max");
		String b = js.getString("high_id");
		int max = Integer.parseInt(a);
		int high = Integer.parseInt(b);
		List<JSONObject> list = mapper.selectuse(uid, b);

		System.out.println(max + "          最大数");
		System.out.println(list.size() + "          长度");

		if (list.size() < max) {
			flag = mapper.upforHigh(cusid, uid, new Date());
			System.out.println("进                来了呀  ");
		}
		System.out.println(flag + "   secrice层            ");
		return flag;
	}

}
